<!--
 * @Author: Hudo
 * @Date: 2021-12-22 16:33:02
 * @LastEditTime: 2022-01-06 15:47:23
 * @LastEditors: hudo
 * @Description: 
 * @FilePath: \am-ui\src\pages\am\engineer\List.vue
-->
<template>
  <div class="engineer">
    <!-- 按钮 -->
    <div class="btns">
      <el-form
        :inline="true"
        :model="params"
        size="small"
        class="demo-form-inline"
      >
        <el-form-item>
          <el-select
            v-model="params.charge_id"
            clearable
            placeholder="请选择区域经理"
          >
            <el-option
              v-for="(item, index) in charges"
              :key="index"
              :label="item.realname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="params.customer_id"
            clearable
            placeholder="请选择客户"
          >
            <el-option
              v-for="(item, index) in customers"
              :key="index"
              :label="item.realname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            @click="toSearchHandler"
            icon="el-icon-search"
            >查询</el-button
          >
        </el-form-item>

        <!-- 添加按钮 -->
        <el-button
          @click="toAddHandler"
          type="primary"
          size="small"
          style="float: right; margin-right: 60px"
          >添加</el-button
        >
      </el-form>
    </div>

    <!-- 表格 -->
    <div class="table">
      <el-table size="small" :data="resData.list" style="width: 100%">
        <el-table-column type="expand">
          <template v-slot="{ row }">
            <!-- 收起内容 -->
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="工程地址:">
                <span>{{ row.address }}</span>
              </el-form-item>
              <el-form-item label="工程经度:">
                <span>{{ row.longitude }}</span>
              </el-form-item>
              <el-form-item label="工程纬度:">
                <span>{{ row.latitude }}</span>
              </el-form-item>
              <el-form-item label="客户姓名:">
                <span>{{ row.customer_name }}</span>
              </el-form-item>
              <el-form-item label="客户电话:">
                <span>{{ row.customer_telephone }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <!-- 展示内容 -->
        <el-table-column
          label="序号"
          type="index"
          align="center"
          fixed
        ></el-table-column>
        <el-table-column
          prop="serial_number"
          label="工程编号"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="name"
          label="工程名称"
          width="300px"
          align="center"
        ></el-table-column>
        <el-table-column prop="type" label="工程类型" align="center">
        </el-table-column>
        <el-table-column prop="charge_name" label="区域经理" align="center">
        </el-table-column>
        <el-table-column label="工程状态" align="center">
          <template v-slot="{ row }">
            <el-tag size="mini" v-if="row.status == '未绑定'" type="danger">{{
              row.status
            }}</el-tag>
            <el-tag
              size="mini"
              v-else-if="row.status == '监测中'"
              type="success"
              >{{ row.status }}</el-tag
            >
            <el-tag size="mini" v-else type="info">{{ row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center">
          <template v-slot="{ row }">
            <el-button @click="toEditHandler(row)" type="text" size="small"
              >编辑</el-button
            >
            <el-button
              :disabled="row.status == '已结束' ? true : false"
              @click="toBindHandler(row)"
              type="text"
              size="small"
              >绑定</el-button
            >
            <el-button
              style="color: red"
              :disabled="row.status == '已结束' ? true : false"
              @click="toEndHandler(row.id)"
              type="text"
              size="small"
              >结束</el-button
            >

            <el-button
              @click="toDeleteHandler(row.id)"
              type="text"
              size="small"
              style="color: red"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页区 -->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="parseInt(resData.page)"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="parseInt(resData.pageSize)"
      layout="total, sizes, prev, pager, next, jumper"
      :total="parseInt(resData.total)"
    >
    </el-pagination>

    <!-- 模态框 -->

    <el-dialog
      @close="dialogCloseHandler"
      :title="title"
      :visible.sync="visible"
    >
      <el-form :inline="true" ref="engineerForm" :rules="rules" :model="form">
        <el-form-item
          prop="name"
          label="工程名称"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item
          prop="serial_number"
          label="工程编号"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.serial_number" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item
          prop="address"
          label="工程地址"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item
          prop="type"
          label="工程类型"
          :label-width="formLabelWidth"
        >
          <el-select v-model="form.type" placeholder="请选择工程类型">
            <el-option label="学校" value="学校"></el-option>
            <el-option label="工地" value="工地"></el-option>
            <el-option label="园区" value="园区"></el-option>
            <el-option label="写字楼" value="写字楼"></el-option>
            <el-option label="住宅" value="住宅"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item
          prop="longitude"
          label="工程经度"
          clearable
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.longitude" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item
          prop="latitude"
          label="工程纬度"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.latitude" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item
          prop="customer_name"
          label="客户姓名"
          :label-width="formLabelWidth"
        >
          <el-input
            :disabled="form.id ? true : false"
            v-model="form.customer_name"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item
          prop="customer_telephone"
          label="客户电话"
          :label-width="formLabelWidth"
        >
          <el-input
            :disabled="form.id ? true : false"
            v-model="form.customer_telephone"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item
          prop="charge_id"
          label="区域经理"
          :label-width="formLabelWidth"
        >
          <el-select v-model="form.charge_id" placeholder="">
            <el-option
              v-for="(item, index) in charges"
              :key="index"
              :label="item.realname"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button @click="toSubmitHandler" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 导入工具插件
import { get, postJSON, del } from "@/utils/request.js";
import _ from "lodash";
export default {
  data() {
    return {
      resData: {},
      params: {
        page: 1,
        pageSize: 10,
      },

      charges: [], //区域经理
      customers: [], //客户
      form: {},
      title: "", // 标题
      visible: false, //模态框默认隐藏
      formLabelWidth: "120px", //表单左侧label的宽度
      rules: {
        name: [
          {
            required: true,
            message: "请输入工程名称",
            trigger: "blur",
          },
        ],
        serial_number: [
          {
            required: true,
            message: "请输入工程编号",
            trigger: "blur",
          },
        ],
        address: [
          {
            required: true,
            message: "请输入工程地址",
            trigger: "blur",
          },
        ],
        type: [
          {
            required: true,
            message: "请输入工程类型",
            trigger: "blur",
          },
        ],
        longitude: [
          {
            required: true,
            message: "请输入工程经度",
            trigger: "blur",
          },
        ],
        latitude: [
          {
            required: true,
            message: "请输入工程纬度",
            trigger: "blur",
          },
        ],
        customer_name: [
          {
            required: true,
            message: "请输入客户姓名",
            trigger: "blur",
          },
        ],
        customer_telephone: [
          {
            required: true,
            message: "请输入客户电话",
            trigger: "blur",
          },
        ],
        charge_id: [
          {
            required: true,
            message: "请选择区域经理",
            trigger: "change",
          },
        ],
      },
    };
  },

  methods: {
    // 搜索按钮点击事件
    toSearchHandler() {
      this.pageQuery();
    },
    //分页查询工程信息
    pageQuery() {
      get("/engineer/pageQuery", this.params).then((res) => {
        // 将后台接口的响应结果赋值给res.data;
        this.resData = res.data;
      });
    },
    // pageSize改变事件
    handleSizeChange(pageSize) {
      // 将分页插件最新的参数赋值给params
      this.params.pageSize = pageSize;
      // 调用查询设备的方法(数据重载)
      this.pageQuery();
    },
    //  page改变事件
    handleCurrentChange(page) {
      // 将分页插件最新的参数赋值给params
      this.params.page = page;
      // 调用查询设备的方法(数据重载)
      this.pageQuery();
    },

    // 查询所有区域经理和客户
    findAllChargesAndCustomers() {
      // 查询区域经理
      get("/user/pageQuery", {
        page: 1,
        pageSize: 100,
        role_id: 3,
      }).then((res) => {
        this.charges = res.data.list;
      });
      // 查询客户
      get("/user/pageQuery", {
        page: 1,
        pageSize: 100,
        role_id: 4,
      }).then((res) => {
        this.customers = res.data.list;
      });
    },

    // 添加按钮点击事件
    toAddHandler() {
      this.title = "添加工程";
      // 表单置空
      this.form = {};
      // this.$refs["engineerForm"].clearValidate();
      // 打开模态框
      this.visible = true;
    },

    // 模态框关闭的回调
    dialogCloseHandler() {
      this.$refs["engineerForm"].clearValidate();
    },

    // 表单提交事件
    toSubmitHandler() {
      postJSON("/engineer/saveOrUpdate", this.form).then((res) => {
        // 数据重载
        this.pageQuery();
        // 成功提示
        this.$message({
          message: "操作成功",
          type: "success",
        });
        // 模态框关闭
        this.visible = false;
      });
    },

    // 编辑按钮点击事件
    toEditHandler(row) {
      this.form = _.clone(row);
      //  修改模态框标题
      this.title = "编辑工程";
      // 打开模态框
      this.visible = true;
    },

    // 绑定按钮点击事件
    toBindHandler(row) {
      // 跳转至工程（绑定）详情页面
      // vue路由的编程式导航
      this.$router.push({
        path: "Details",
        query: row,
      });
    },

    // 结束按钮点击事件
    toEndHandler(id) {
      this.$confirm("此操作将结束该工程，是否继续", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        // 用户点击确定按钮，然后再做设备的删除
        get("/engineer/finish", { id }).then((res) => {
          // 数据重载
          this.pageQuery();
          // 提示删除成功
          this.$message.success("工程结束成功");
        });
      });
    },

    // 删除按钮点击事件
    toDeleteHandler(id) {
      this.$confirm("此操作将永久删除该设备，是否继续", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        // 用户点击确定按钮，然后再做设备的删除
        del("/engineer/deleteById", { id }).then((res) => {
          // 数据重载
          this.pageQuery();
          // 提示删除成功
          this.$message.success("删除成功");
        });
      });
    },

    // 查询按钮点击事件
    toSearchHandler() {
      // 数据重载
      this.pageQuery();
    },

    // 表单确定按钮点击事件

    toSubmitHandler() {
      postJSON("/engineer/saveOrUpdate", this.form).then((res) => {
        // 数据重载
        this.pageQuery();
        this.$message.success("操作成功");
        this.visible = falese;
      });
    },
  },

  // vue生命周期的钩子函数
  created() {
    // 页面加载
    this.pageQuery();
    // 查询所有区域经理和客户
    this.findAllChargesAndCustomers();
  },
};
</script>